package com.lzx.adx.report.util.biz.product;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class CzChannelHelper {


    /**
     * 创智封装
     * @param accountDTO
     * @param reportDTO
     * @return 响应封装的地址
     */
    public static String covertCzReport(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String,Object> params=new HashMap<>();

        if (StringUtils.isNotBlank(reportDTO.getOaid()) && !reportDTO.getOaid().contains("__")) {
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !reportDTO.getOaidSum().contains("__")) {
            params.put("oaidmd5", reportDTO.getOaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) && !reportDTO.getHash_android_id().contains("__")) {
            params.put("android_id", reportDTO.getHash_android_id());
        }
        if(StringUtils.isNotBlank(reportDTO.getImei()) && !reportDTO.getImei().contains("__")) {
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !reportDTO.getImeiSum().contains("__")) {
            params.put("imeimd5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) && !reportDTO.getIdfa().contains("__")){
            params.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) && !reportDTO.getIdfaSum().contains("__")){
            params.put("idfaMd5", reportDTO.getIdfaSum());
        }

        params.put("mac","");

        params.put("ip",reportDTO.getIp());
        params.put("user_agent", URLUtil.encode(reportDTO.getUser_agent()));
        params.put("srequest_id", reportDTO.getBizRequestId());
        String callbackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());
        String urlParams= URLUtil.buildQuery(params, StandardCharsets.UTF_8)+"&callback="+ URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl()+"&"+urlParams;
    }
}
